7.4 - Fixing asset bundles shaders


To fix asset bundles GameObjects being completely black we need to do the following on each part of it which has the MeshRenderer component:


var tank = UnityEngine.Object.Instantiate(Tank.TankPrefab, SonsTools.GetPositionInFrontOfPlayer(8, 2), Quaternion.Euler(Vector3.zero)); // spawning the tank

tank.GetChildren().ForEach(child => {

    var meshRend = child.GetComponent<MeshRenderer>(); // trying to get the MeshRenderer component from the child GameObject
    if (meshRend) // evaluating if the child GameObject  the MeshRenderer component
    {
        // if so we apply a new shader
        meshRend.sharedMaterial.shader = Shader.Find("Sons/HDRPLit"); // applying the Sons/HDRPLit shader to the child GameObject of the tank
    }
});

tank.transform.Find("turret/muzzle").GetComponent<MeshRenderer>().sharedMaterial.shader = Shader.Find("Sons/HDRPLit"); // applying the Sons/HDRPLit shader to a deep nested child GameObject
            

As you can see, most of the time to fix the black texture problem we just need to apply the Sons/HDRPLit shader to the child GameObjects which have the MeshRenderer component.